<?php
/**
 * Created by PhpStorm.
 * User: hongyeah
 * Date: 2015/10/21
 * Time: 22:18
 */

namespace Admin\Controller;


use Think\Controller;
use Think\Exception;

class UsersController extends Controller
{

    public function listUsers(){
        /*$name = "张时";
        $cardnum = "201330345555";
        $password = "0000";
        $department = "软件学院";
        $major = "软件工程";
        $sex = 1;
        $data = array('name' => $name,
            'cardnum' => $cardnum,
        'password' => $password,
            'department' => $department,
            'major' => $major,
            'sex' => $sex
    );
        $result = D('Users')->addUsers($data);*/
//        $result = D('Users')->getUsersList("1");
//        dump($result);
        $this->display();
    }

    public function addMessage(){
        $this->display();
    }

    public function addUsers(){
        $cardnum = I('post.cardnum');
        $name = I('post.name');
        $password = I('post.password');
        $department = I('post.department');
        $major = I('post.major');
        $sex = I('post.sex');

        $data = array(
            'name'          => $name,
            'cardnum'       => $cardnum,
            'password'       => $password,
            'department'    => $department,
            'major'         => $major,
        );

        if($sex=='男') {
            $data['sex'] = 1;
        }else{
            $data['sex'] = 0;
        }

        $users_model = D('Users');


        $result = $users_model->addUsers($data);

        $this->ajaxReturn($result);
    }

    public function getUsersList(){
        $search = I('search/s');
        $users_model = D('Users');

        try{
            $data = $users_model->getUsersList($search);
        }catch (Exception $e){
            $this->ajaxReturn(array('state'=>'0','show'=>0));
        }

        $data->state = 1;

        $this->ajaxReturn($data);
    }

    public function delUsersById(){
        $ids = I('ids');

        $this->ajaxReturn(D('Users')->delUsersById($ids));

    }

    public function saveUsers(){
        $name = I('name');
        $cardnum = I('cardnum');
        $department = I('department');
        $major = I('major');
        $sex = I('sex');
        $data = array(
            'name'          => $name,
            'cardnum'       => $cardnum,
            'department'    => $department,
            'major'         => $major,
        );

        if($sex=='男') {
            $data['sex'] = 1;
        }else{
            $data['sex'] = 0;
        }

        $users_model = D('Users');

        $result = $users_model->edtUsers($data);

        $this->ajaxReturn($result);
    }

    public function usersMessage(){
        $id = $_GET['id'];
        $result = D('Users')->getUsersByID($id);
        if($result['sex']==1) $result['sex']='男';
        else  $result['sex']='女';
        $this->assign('user',$result);
        $this->display();
    }
    public function editMessage(){
        $id = $_GET['id'];
        $result = D('Users')->getUsersByID($id);
        if($result['sex']==1) $result['sex']='男';
        else  $result['sex']='女';
        $this->assign('user',$result);
        $this->display();
    }
}